﻿// CaptureDlg.cpp: 实现文件
//

#include "pch.h"
#include "CaptureDlg.h"
#include "ProjectAA.h"
#include "afxdialogex.h"


// CCaptureDlg 对话框

IMPLEMENT_DYNAMIC(CCaptureDlg, CDialogEx)

CCaptureDlg::CCaptureDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOG_CAPTURE, pParent)
	, m_bWriteiic(FALSE)
	, m_bMirrorX(FALSE)
	, m_bMirrorY(FALSE)
	, m_nCTSId(0)
	, m_nGpioFlag(FALSE)
	, m_bFlag1(FALSE)
	, m_bScanX(FALSE)
	, m_bScanY(FALSE)
	, m_nScanDis(0)
	, m_nThreshold(0)
	, m_nFilter(0)
	, m_bFlag2(FALSE)
	, m_dHigh(0)
	, m_dLow(0)
	, PassFlag(FALSE)
	, PassX(0)
	, PassY(0)
	, PassW(0)
	, PassH(0)
	, LightFlag(FALSE)
	, LightX(0)
	, LightY(0)
	, LightW(0)
	, LightH(0)
	, BeforePlasmaFlag(FALSE)
	, AfterPlasmaFlag(FALSE)
	, AfterGlueFlag(FALSE)
	, SaveOrgFlag(FALSE)
	, VersionFlag(FALSE)
{

}

CCaptureDlg::~CCaptureDlg()
{
}

void CCaptureDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_IMAGE_SOURCE, m_comboImageSource);
	DDX_Control(pDX, IDC_COMBO_TEST_MODE, m_comboTestMode);
	DDX_Control(pDX, IDC_COMBO_CAPTURE_MODE, m_comboCaptureMode);
	DDX_Control(pDX, IDC_COMBO_CHANGE_MODE, m_comboChangeMode);
	DDX_Check(pDX, IDC_CHECK_WRITE_IIC, m_bWriteiic);
	DDX_Check(pDX, IDC_CHECK_MIRROR_X, m_bMirrorX);
	DDX_Check(pDX, IDC_CHECK_MIRROR_Y, m_bMirrorY);
	DDX_Text(pDX, IDC_EDIT_CTS_ID, m_nCTSId);
	DDX_Check(pDX, IDC_CHECK_GPIO, m_nGpioFlag);
	DDX_Control(pDX, IDC_COMBO_GPIO, m_comboGpioNo);

	DDX_Check(pDX, IDC_CHECK3, m_bFlag1);
	DDX_Check(pDX, IDC_CHECK7, m_bScanX);
	DDX_Check(pDX, IDC_CHECK8, m_bScanY);
	DDX_Text(pDX, IDC_EDIT2, m_nScanDis);
	DDX_Text(pDX, IDC_EDIT18, m_nThreshold);
	DDX_Text(pDX, IDC_EDIT19, m_nFilter);
	DDX_Check(pDX, IDC_CHECK9, m_bFlag2);
	DDX_Text(pDX, IDC_EDIT20, m_dHigh);
	DDX_Text(pDX, IDC_EDIT21, m_dLow);
	DDX_Check(pDX, IDC_CHECK1, PassFlag);
	DDX_Text(pDX, IDC_EDIT1, PassX);
	DDX_Text(pDX, IDC_EDIT22, PassY);
	DDX_Text(pDX, IDC_EDIT8, PassW);
	DDX_Text(pDX, IDC_EDIT23, PassH);
	DDX_Check(pDX, IDC_CHECK14, LightFlag);
	DDX_Text(pDX, IDC_EDIT25, LightX);
	DDX_Text(pDX, IDC_EDIT26, LightY);
	DDX_Text(pDX, IDC_EDIT27, LightW);
	DDX_Text(pDX, IDC_EDIT28, LightH);
	DDX_Check(pDX, IDC_CHECK5, BeforePlasmaFlag);
	DDX_Check(pDX, IDC_CHECK6, AfterPlasmaFlag);
	DDX_Check(pDX, IDC_CHECK10, AfterGlueFlag);
	DDX_Check(pDX, IDC_CHECK11, SaveOrgFlag);
	DDX_Check(pDX, IDC_CHECK_WRITE_IIC2, VersionFlag);
}


BEGIN_MESSAGE_MAP(CCaptureDlg, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON2, &CCaptureDlg::OnBnClickedButton2)
	ON_BN_CLICKED(IDC_BUTTON1, &CCaptureDlg::OnBnClickedButton1)
END_MESSAGE_MAP()


// CCaptureDlg 消息处理程序


BOOL CCaptureDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	ReadConfigIni();

	m_comboImageSource.ResetContent();
	m_comboImageSource.AddString(L"0: 本地文件");
	m_comboImageSource.AddString(L"1: CTS采集卡");
	m_comboImageSource.AddString(L"2: 软龙格采集卡");
	m_comboImageSource.SetCurSel(gSystemMgr.m_nImageSource);

	m_comboTestMode.ResetContent();
	m_comboTestMode.AddString(L"0: 原图显示");
	m_comboTestMode.AddString(L"1: 白场测试");
	m_comboTestMode.SetCurSel(gSystemMgr.m_nTestMode);

	m_comboCaptureMode.ResetContent();
	m_comboCaptureMode.AddString(L"0: 单张");
	m_comboCaptureMode.AddString(L"1: 连续");
	m_comboCaptureMode.SetCurSel(gSystemMgr.m_nCaptureMode);

	m_comboChangeMode.ResetContent();
	m_comboChangeMode.AddString(L"0: 原图");
	m_comboChangeMode.AddString(L"1: UYVY");
	m_comboChangeMode.AddString(L"2: VYUY");
	m_comboChangeMode.AddString(L"3: YVYU");
	m_comboChangeMode.AddString(L"4: YUYV");
	m_comboChangeMode.AddString(L"5: BGGR");
	m_comboChangeMode.AddString(L"6: RGGB");
	m_comboChangeMode.AddString(L"7: GBRG");
	m_comboChangeMode.AddString(L"8: GRBG");
	m_comboChangeMode.SetCurSel(gSystemMgr.m_nImageChangeMode);

	m_comboGpioNo.ResetContent();
	m_comboGpioNo.AddString(L"0");
	m_comboGpioNo.AddString(L"1");
	m_comboGpioNo.AddString(L"2");
	m_comboGpioNo.AddString(L"3");
	m_comboGpioNo.SetCurSel(gSystemMgr.m_nGpioNo);

	CWnd* ctrl = GetTopWindow();
	while (ctrl != NULL)
	{
		ctrl->EnableWindow(gSystemMgr.isAdmin ? TRUE : FALSE);
		ctrl = ctrl->GetNextWindow();
	}
	if (!gSystemMgr.isAdmin)
	{
		
	}


	return TRUE;
}


void CCaptureDlg::OnCancel()
{
	CDialogEx::OnCancel();
}


void CCaptureDlg::OnBnClickedButton2()
{
	SaveValueToConfig();
}

void CCaptureDlg::ReadConfigIni()
{
	(gSystemMgr.m_bWriteIIC == 1) ? m_bWriteiic = true : m_bWriteiic = false;
	(gSystemMgr.m_bMirrorX == 1) ? m_bMirrorX = true : m_bMirrorX = false;
	(gSystemMgr.m_bMirrorY == 1) ? m_bMirrorY = true : m_bMirrorY = false;
	m_nCTSId = gSystemMgr.m_nCtsId;
	(gSystemMgr.m_bGpioFlag == 1) ? m_nGpioFlag = true : m_nGpioFlag = false;

	m_bFlag1 = gSystemMgr.m_nStainTestFlag;
	m_bScanX = gSystemMgr.m_nScanXFlag;
	m_bScanY = gSystemMgr.m_nScanYFlag;
	m_nScanDis = gSystemMgr.m_nScanDistance;
	m_nThreshold = gSystemMgr.m_nScanThreshold;
	m_nFilter = gSystemMgr.m_nFilterClose;

	m_bFlag2 = gSystemMgr.m_nBadPointTestFlag;
	m_dHigh = gSystemMgr.m_fHighLimit;
	m_dLow = gSystemMgr.m_fLowLimit;

	PassFlag = gSystemMgr.PassFlag;
	PassX = gSystemMgr.PassX;
	PassY = gSystemMgr.PassY;
	PassW = gSystemMgr.PassW;
	PassH = gSystemMgr.PassH;

	LightFlag = gSystemMgr.LightFlag;
	LightX = gSystemMgr.LightX;
	LightY = gSystemMgr.LightY;
	LightW = gSystemMgr.LightW;
	LightH = gSystemMgr.LightH;

	BeforePlasmaFlag = gSystemMgr.BeforePlasmaFlag;
	AfterPlasmaFlag = gSystemMgr.AfterPlasmaFlag;
	AfterGlueFlag = gSystemMgr.AfterGlueFlag;

	SaveOrgFlag = gSystemMgr.SaveOrgFlag;

	VersionFlag = gSystemMgr.VersionFlag;

	UpdateData(FALSE);
}

void CCaptureDlg::SaveValueToConfig()
{
	UpdateData(TRUE);

	gSystemMgr.m_nStainTestFlag = m_bFlag1;
	gSystemMgr.m_nScanXFlag = m_bScanX;
	gSystemMgr.m_nScanYFlag = m_bScanY;
	gSystemMgr.m_nScanDistance = m_nScanDis;
	gSystemMgr.m_nScanThreshold = m_nThreshold;
	gSystemMgr.m_nFilterClose = m_nFilter;
	gSystemMgr.m_nBadPointTestFlag = m_bFlag2;
	gSystemMgr.m_fHighLimit = m_dHigh;
	gSystemMgr.m_fLowLimit = m_dLow;

	gSystemMgr.PassFlag = PassFlag;
	gSystemMgr.PassX = PassX;
	gSystemMgr.PassY = PassY;
	gSystemMgr.PassW = PassW;
	gSystemMgr.PassH = PassH;

	gSystemMgr.LightFlag = LightFlag;
	gSystemMgr.LightX = LightX;
	gSystemMgr.LightY = LightY;
	gSystemMgr.LightW = LightW;
	gSystemMgr.LightH = LightH;

	gSystemMgr.BeforePlasmaFlag = BeforePlasmaFlag;
	gSystemMgr.AfterPlasmaFlag = AfterPlasmaFlag;
	gSystemMgr.AfterGlueFlag = AfterGlueFlag;

	gSystemMgr.SaveOrgFlag = SaveOrgFlag;

	gSystemMgr.VersionFlag = VersionFlag;

	CString cstr;
	cstr.Format(L"%d", gSystemMgr.VersionFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"VersionFlag", cstr);

	cstr.Format(L"%d", gSystemMgr.SaveOrgFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"SaveOrgFlag", cstr);

	cstr.Format(L"%d", gSystemMgr.BeforePlasmaFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"BeforePlasmaFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.AfterPlasmaFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"AfterPlasmaFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.AfterGlueFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"AfterGlueFlag", cstr);

	cstr.Format(L"%d", gSystemMgr.LightFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"LightFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.LightX); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"LightX", cstr);
	cstr.Format(L"%d", gSystemMgr.LightY); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"LightY", cstr);
	cstr.Format(L"%d", gSystemMgr.LightW); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"LightW", cstr);
	cstr.Format(L"%d", gSystemMgr.LightH); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"LightH", cstr);

	cstr.Format(L"%d", gSystemMgr.PassFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"PassFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.PassX); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"PassX", cstr);
	cstr.Format(L"%d", gSystemMgr.PassY); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"PassY", cstr);
	cstr.Format(L"%d", gSystemMgr.PassW); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"PassW", cstr);
	cstr.Format(L"%d", gSystemMgr.PassH); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"PassH", cstr);


	cstr.Format(L"%d", gSystemMgr.m_nStainTestFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_nStainTestFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nScanXFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_nScanXFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nScanYFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_nScanYFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nScanDistance); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_nScanDistance", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nScanThreshold); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_nScanThreshold", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nFilterClose); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_nFilterClose", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nBadPointTestFlag); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_nBadPointTestFlag", cstr);
	cstr.Format(L"%.2f", gSystemMgr.m_fHighLimit); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_fHighLimit", cstr);
	cstr.Format(L"%.2f", gSystemMgr.m_fLowLimit); gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"WhiteSetting", L"m_fLowLimit", cstr);


	(m_bWriteiic == TRUE) ? gSystemMgr.m_bWriteIIC = 1 : gSystemMgr.m_bWriteIIC = 0;
	(m_bMirrorX == TRUE) ? gSystemMgr.m_bMirrorX = 1 : gSystemMgr.m_bMirrorX = 0;
	(m_bMirrorY == TRUE) ? gSystemMgr.m_bMirrorY = 1 : gSystemMgr.m_bMirrorY = 0;
	gSystemMgr.m_nImageSource = m_comboImageSource.GetCurSel();
	gSystemMgr.m_nTestMode = m_comboTestMode.GetCurSel();
	gSystemMgr.m_nCaptureMode = m_comboCaptureMode.GetCurSel();
	gSystemMgr.m_nImageChangeMode = m_comboChangeMode.GetCurSel();
	gSystemMgr.m_nCtsId = m_nCTSId;
	(m_nGpioFlag == TRUE) ? gSystemMgr.m_bGpioFlag = 1 : gSystemMgr.m_bGpioFlag = 0;
	gSystemMgr.m_nGpioNo = m_comboGpioNo.GetCurSel();

	cstr.Format(L"%d", gSystemMgr.m_bWriteIIC);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_bWriteIIC", cstr);
	cstr.Format(L"%d", gSystemMgr.m_bMirrorX);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_bMirrorX", cstr);
	cstr.Format(L"%d", gSystemMgr.m_bMirrorY);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_bMirrorY", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nImageSource);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_nImageSource", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nTestMode);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_nTestMode", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nCaptureMode);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_nCaptureMode", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nImageChangeMode);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_nImageChangeMode", cstr);

	cstr.Format(L"%d", gSystemMgr.m_nCtsId);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_nCtsId", cstr);

	cstr.Format(L"%d", gSystemMgr.m_bGpioFlag);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_bGpioFlag", cstr);
	cstr.Format(L"%d", gSystemMgr.m_nGpioNo);
	gSystemMgr.m_cCaptureConfigIni.SaveConfigIni(L"CaptureConfig", L"m_nGpioNo", cstr);

	switch (gSystemMgr.m_nImageSource) {
	case IMG_DEV_FILE:
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgFile;
		//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"Image Files");
		break;
	case IMG_DEV_CTS:
		if (gSystemMgr.m_bCTSInitFlag == 0) {
			Cam_Init();
			gSystemMgr.m_bCTSInitFlag = 1;
		}
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgCTS;
		//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"CTS Cam");
		break;
	case IMG_RO_LONGO:
		gSystemMgr.imgCap = (ImgCap *)&gSystemMgr.imgRLG;
		//SendMessage(HWND_STATUS, SB_SETTEXT, 2, (LPARAM)"RLG Cam");
		break;
	}

	gSystemLog.DisplayLogAndSave(L"Capture保存数据完成", LV_EVENT);
}


void CCaptureDlg::OnBnClickedButton1()
{
	switch (gSystemMgr.m_nImageSource)
	{
	case IMG_DEV_CTS:
		gSystemMgr.imgCTS.write_iic();
		break;
	case IMG_RO_LONGO:
		gSystemMgr.imgRLG.write_iic();
		break;
	}
}
